package com.lucenten.collect.gnss.runnable;

import cn.quevo.common.lang.NumberUtils;
import cn.quevo.common.lang.StringUtils;
import cn.quevo.core.spring.util.PropertiesUtils;
import com.lucenten.collect.config.entity.dto.ThreadStation;
import com.lucenten.collect.config.entity.query.CollectConfQuery;
import com.lucenten.collect.config.service.CollectConfService;
import com.lucenten.collect.gnss.entity.SatelliteStation;
import com.lucenten.collect.gnss.entity.query.SatelliteStationQuery;
import com.lucenten.collect.gnss.handler.tools.GnssContext;
import com.lucenten.collect.gnss.service.SatelliteStationService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Data
@Slf4j
public abstract class AbstractGnssUnZipRunService implements GnssUnZipRunService {
    protected final PropertiesUtils props = new PropertiesUtils("/conf/gnss.properties");
    protected final String stationCode = props.getProperties("collect.station.code");
    protected final Integer THREAD_NUM = NumberUtils.toInt(props.getProperties("collect.station.thread.num", "200"));
    protected ExecutorService fixedThreadPool = Executors.newFixedThreadPool(THREAD_NUM);

    @Resource(name = "collectConfService")
    private CollectConfService collectConfService;
    @Resource(name = "satelliteStationService")
    private SatelliteStationService satelliteStationService;

    /** 当前线程信息 */
    private ThreadStation threadStation;
    /** 所有站点信息 */
    private List<SatelliteStation> satelliteStationList;
    /** 以stationId+,+stationName为key的所有站点信息 */
    private Map<String,SatelliteStation> stationMap = new HashMap<>();
    /** 以stationId+,+reStationName为key的所有站点信息 */
    private Map<String,SatelliteStation> reStationMap = new HashMap<>();

    @Override
    public void run(String root) {
        this.threadStation = collectConfService.getCollectConfByCode(CollectConfQuery.builder().serverCode(stationCode).build());
        if (threadStation == null) {
            log.debug(stationCode + " 线程尚未开启");
            return;
        }
        this.satelliteStationList = this.satelliteStationService.getSatelliteStationList(SatelliteStationQuery.builder().build());
        if (StringUtils.isEmpty(this.satelliteStationList)) {
            log.debug(stationCode + " 暂无站点信息");
            return;
        }
        this.convertMap();
        this.startThread(Paths.get(root));
    }

    /**
     * List转Map
     */
    protected void convertMap() {
        for (SatelliteStation satelliteStation : this.satelliteStationList) {
            this.stationMap.put(satelliteStation.getStationId()+","+satelliteStation.getStationName(), satelliteStation);
            this.reStationMap.put(satelliteStation.getReStationName(), satelliteStation);
        }
    }

    protected abstract void startThread(Path root);

    /**
     * 验证是否需要处理
     *
     * @param file
     * @return
     */
    protected boolean isProcess(Path file){
        // 如果不是.t02、.zip、d.z的格式则直接忽略
        return GnssContext.isZipExt(file.getFileName().normalize().toString());
    }

}
